home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / M / May MacUser Program.cpt / miniGenApp Src / WindowUtil.c < prev   
Encoding:
C/C++ Source or Header  |  1990-03-29  |  3.6 KB  |  116 lines  |  [TEXT/KAHL]

  1. /* *****************************************************************************
  2.     FILE:             WindowUtil.c
  3.     
  4.     DESCRIPTION:     Window management utilities
  5.  
  6.     AUTHOR:            Kurt W.G. Matthies
  7.         
  8.     Copyright © 1990 by Code of the West, Inc., All Rights Reserved.
  9.     
  10.     Revision History:
  11.     ==========================================================
  12.     3.30.90    -    May 1990 MacUser Release
  13.     ==========================================================
  14.  
  15.    ***************************************************************************** */
  16.  
  17. #include "WindowUtilPr.h"
  18.  
  19. /* ----------------------------------------------------------------------------
  20.     centerWindow -    calculate the left, top position of a centered window
  21.     3.30.90kwgm        and move it to there
  22. ------------------------------------------------------------------------------- */
  23. void
  24. centerWindow (theWindow)
  25.     WindowPtr        theWindow;
  26. {
  27.     short            scrLength, scrHeight;
  28.     Rect            globalRect;
  29.     Point            where;
  30.  
  31.     /* get the window rect in global coordinates */
  32.     getWinRect (theWindow, &globalRect);
  33.     
  34.     /* get the screen size */
  35.     scrLength = screenBits.bounds.right - screenBits.bounds.left;
  36.     scrHeight = (screenBits.bounds.bottom - screenBits.bounds.top) - GetMBarHeight();
  37.  
  38.     where.v = (scrHeight - (globalRect.bottom - globalRect.top)) / 2;
  39.     where.h = (scrLength - (globalRect.right - globalRect.left)) / 2;
  40.     
  41.     MoveWindow (theWindow, where.h, where.v, false);
  42.     
  43. } /* centerWindow */
  44.     
  45. /*-----------------------------------------------------------------------------
  46.     getWinRect -    returns window portRect in global coordinates
  47.     3.30.90kwgm        used for centering the window
  48. ----------------------------------------------------------------------------- */
  49. Rect *
  50. getWinRect (theWindow, globalRect)
  51.     WindowPtr        theWindow;
  52.     Rect            *globalRect;
  53. {
  54.     GrafPtr        savePort;
  55.     
  56.     GetPort (&savePort);
  57.     SetPort (theWindow);
  58.     
  59.     /* 
  60.         when passed a pointer to and object, as in Rect *globalRect, 
  61.         you need to dereference the pointer to store
  62.         the data in the object, as in the following line:
  63.     */
  64.     
  65.     *globalRect = theWindow->portRect;
  66.     
  67.     /* now hang on to you hats! */
  68.     LocalToGlobal (&(topLeft(*globalRect)));    /* convert top-left */
  69.     LocalToGlobal (&(botRight(*globalRect)));    /* convert bottom-right */
  70.     
  71. #if 0
  72.         these two lines of code need explaination:
  73.         
  74.         the macros topLeft and botRight are defined in MacTypes.h
  75.         they return either the top-left or bottom-right points 
  76.         of a rectangle, in a way that looks like a Point data structure.
  77.         
  78.         Now, lets look at one of these lines from the inside out:
  79.         
  80.         Remember, you need to dereference a pointer to get at it's value,
  81.         thus the *globalRect. And LocalToGlobal takes a VAR parameter,
  82.         so we need to pass an address, thus the &
  83.         
  84.         Let's look at it in another, more readable way to do it:
  85.         
  86.         Point        aPt;        /* need to declare an auto Point */
  87.         
  88.         aPt.h = globalRect->left;    /* get top-left */
  89.         aPt.v = globalRect->top;
  90.         LocalToGlobal (&aPt);        /* convert it */
  91.         globalRect->left = aPt.h;
  92.         globalRect->top = aPt.v;
  93.         
  94.         aPt.h = globalRect->right;    /* get bottom-right */
  95.         aPt.v = globalRect->bottom;
  96.         LocalToGlobal (&aPt);        /* convert it */
  97.         globalRect->right = aPt.h;
  98.         globalRect->bottom = aPt.v;
  99.         
  100.         These ten lines do what the two line above do.
  101.         
  102.         Can you now appreciate why sometimes, and I underline the word 'sometimes',
  103.         writing terse code is preferable?
  104.         
  105. #endif
  106.     
  107.     SetPort (savePort);
  108.     
  109.     return (globalRect);
  110.     
  111. } /* getWinRect */
  112.  
  113. /* ===============================  EOF  =======================================
  114.     Copyright © 1990 by Code of the West, Inc. All Rights Reserved.
  115. ================================================================================ */
  116.